package com.wzcl.app.controller.student;

import com.wzcl.app.dao.model.Course;
import com.wzcl.app.dao.model.UserCourse;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.UserCourseDto;
import com.wzcl.app.model.dto.UserCoursePageDto;
import com.wzcl.app.model.vo.UserCourseVo;
import com.wzcl.app.service.UserCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/userCourse")
@Api(value = "用户课程管理", tags = {"用户课程管理接口"})
public class UserCourseController {

    @Resource
    private UserCourseService service;

    @ApiOperation("用户课程list")
    @PostMapping("/getList")
    public RVO<List<UserCourseVo>> getCourseList(@RequestBody UserCourseDto userCourseDto){
        userCourseDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        List<UserCourse> list = service.getUserCourseList(userCourseDto);
        if (list != null && list.size() > 0) {
            List<UserCourseVo> collect = list.stream().map(UserCourseVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("用户课程page")
    @PostMapping("/getPage")
    public  RVO<List<UserCourseVo>> getCoursePage(@RequestBody UserCourseDto userCourseDto){
        userCourseDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        MyPage<UserCourse> page = service.getUserCoursePage(userCourseDto);
        if (page != null && page.getContent().size()>0) {
            List<UserCourseVo> collect = page.getContent().stream().map(UserCourseVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
        return  RVO.success(null);
    }

    @ApiOperation("用户课程分页pages")
    @PostMapping("/getPages")
    public  RVO getUserCoursePage(@RequestBody UserCoursePageDto userCoursePageDto){
        String userCourse = service.getUserCourse(userCoursePageDto);
        return RVO.success(userCourse);
    }

    @ApiOperation("查询指定用户已购买的课程")
    @PostMapping("/retrieve/userCourse/{userId}")
    public  RVO retrieveUserCourseByUserId(@PathVariable Integer userId){
        List<Course> courseList = service.retrieveUserCourseByUserId(userId);
        return RVO.success(courseList);
    }
}
